package cate.game.doc;

import cate.common.table.XSSFRowWrap;
import easy.java.codec.JsonCodec;
import easy.java.io.file.FileWrite;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class PayConfigDocMaker {

    private static final String excelPath = "E:\\project_cate\\repository\\放置美食\\策划\\上传文件\\excel\\国内主版本\\";

    static class PayItemConfig {
        public int payId;
        public String payName;
    }

    public static void main(String[] args) {
        File excelFile = new File(excelPath + "支付.xlsx");
        if (!excelFile.exists()) {
            return;
        }
        List<PayItemConfig> configs = new LinkedList<>();
        try {
            FileInputStream fis = new FileInputStream(excelFile);
            XSSFWorkbook workbook = new XSSFWorkbook(fis);
            XSSFSheet sheet = workbook.getSheet("充值项");
            XSSFRow headerRow = sheet.getRow(0);
            final int beg = headerRow.getFirstCellNum();
            final int end = headerRow.getLastCellNum();
            Map<String, Integer> fieldIndexMap = new HashMap<>();
            for (int i = beg; i < end; i++) {
                XSSFCell cell = headerRow.getCell(i);
                if (cell == null) {
                    continue;
                }
                String cellVal = cell.getStringCellValue().replace("*", "");
                fieldIndexMap.put(cellVal, i);
            }

            for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getLastRowNum(); i++) {
                XSSFRow row = sheet.getRow(i);
                if (row == null) {
                    continue;
                }

                if (i == sheet.getFirstRowNum() + 1) {
                    try {
                        XSSFCell cell = row.getCell(0);
                        if (cell != null && cell.getCellType() == CellType.STRING) {
                            String testMark = cell.getStringCellValue();
                            if (StringUtils.isNotBlank(testMark) && StringUtils.startsWith(testMark, "#")) {
                                continue;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                XSSFRowWrap xrw = new XSSFRowWrap(row, fieldIndexMap);
                Integer id = xrw.getInteger("ID");
                String name = xrw.getString("名称");
                if (id == null || name == null) {
                    continue;
                }
                PayItemConfig config = new PayItemConfig();
                config.payId = id;
                config.payName = name;
                configs.add(config);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            byte[] jsonBytes = JsonCodec.instance().encodeBytes(configs);
            FileWrite.i.writeBytes(".tmp/pay.config.json", jsonBytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
